package com.tudonovelas;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.MalformedURLException;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;

import com.facebook.android.AsyncFacebookRunner;
import com.facebook.android.AsyncFacebookRunner.RequestListener;
import com.facebook.android.DialogError;
import com.facebook.android.Facebook;
import com.facebook.android.Facebook.DialogListener;
import com.facebook.android.FacebookError;
import com.tudonovelas.SessionEvents.AuthListener;
import com.tudonovelas.SessionEvents.LogoutListener;

public class FacebookConnector {
	

	private Facebook facebook = null;
	private Context context;
	private String[] permissions;
	private Handler mHandler;
	private Activity activity;
	private SessionListener mSessionListener = new SessionListener();
	private MaAuthListener maAuthListener = new MaAuthListener(); 
	private MaLogoutListener maLogoutListener = new MaLogoutListener();
	

	public FacebookConnector(String appId,Activity activity,Context context,String[] permissions) {
		this.facebook = new Facebook(appId);

		SessionStore.restore(facebook, context);
        SessionEvents.addAuthListener(maAuthListener);
        SessionEvents.addLogoutListener(maLogoutListener);
        
		this.context=context;
		this.permissions=permissions;
		this.mHandler = new Handler();
		this.activity=activity;
	}

	public void login() {
        if (!facebook.isSessionValid()) {
            facebook.authorize(this.activity, this.permissions,new LoginDialogListener());
        }
    }
	

	public void postMessageOnWall(String msg) {
		if (facebook.isSessionValid()) {
		    Bundle parameters = new Bundle();
		    parameters.putString("message", msg);
		    try {
				String response = facebook.request("me/feed", parameters,"POST");
				System.out.println(response);
			} catch (IOException e) {
				e.printStackTrace();
			}
		} else {
			login();
		}
	}	

	public void logout() {
        SessionEvents.onLogoutBegin();
        AsyncFacebookRunner asyncRunner = new AsyncFacebookRunner(this.facebook);
        asyncRunner.logout(this.context, new LogoutRequestListener());
	}
	
	 private final class LoginDialogListener implements DialogListener {
	        public void onComplete(Bundle values) {
	            SessionEvents.onLoginSuccess();
	        }

	      

			@Override
			public void onFacebookError(FacebookError error) {
				SessionEvents.onLoginError(error.getMessage());
				
			}

			@Override
			public void onError(DialogError error) {
				SessionEvents.onLoginError(error.getMessage());
				
			}



			@Override
			public void onCancel() {
				SessionEvents.onLoginError("Action Canceled");
				
			}
	    }
	 
	 public class LogoutRequestListener implements RequestListener{
	        public void onComplete(String response, final Object state) {
	            // callback should be run in the original thread, 
	            // not the background thread
	            mHandler.post(new Runnable() {
	                public void run() {
	                    SessionEvents.onLogoutFinish();
	                }
	            });
	        }

			@Override
			public void onIOException(IOException e, Object state) {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void onFileNotFoundException(FileNotFoundException e,
					Object state) {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void onMalformedURLException(MalformedURLException e,
					Object state) {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void onFacebookError(FacebookError e, Object state) {
				// TODO Auto-generated method stub
				
			}
	    }
	
	 private class SessionListener {
	        
	        public void onAuthSucceed() {
	            SessionStore.save(facebook, context);
	        }

	        public void onAuthFail(String error) {
	        }
	        
	        public void onLogoutBegin() {           
	        }
	        
	        public void onLogoutFinish() {
	            SessionStore.clear(context);
	        }
	    }

		public Facebook getFacebook() {
			return this.facebook;
		}

}
